/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Logika;

/**
 *
 * @author mejcu
 */
public class Register {

    /**
     *
     */
    public static final int SIZE = 4;
    private String[] registerString = new String[SIZE];
    /**
     *
     */
    public int[] registerInt = {-1, -1, -1, -1};
    private int currentPointer = 0;
    private boolean deleted = false;

    /**
     *
     */
    public Register() {
    }

    /**
     *
     * @param clientAdress
     * @return
     */
    public int add(String clientAdress) {
        if (deleted) {
            return addToDifferentPlace(clientAdress);
        } else {
            registerString[currentPointer] = clientAdress;
            registerInt[currentPointer] = 1;
            System.out.println("Dodano na pozycje " + currentPointer);
            currentPointer++;
        }
        return currentPointer;
    }

    /**
     *
     * @param clientAdress
     */
    public void remove(String clientAdress) {
        for (int i = 0; i < currentPointer; i++) {
            if (registerString[i].compareTo(clientAdress) == 0) {
                registerString[i] = null;
                registerInt[i] = -1;
                deleted = true;
                System.out.println("-----------------------------------------");
                System.out.println("Usunieto " + clientAdress);
                if (i == (currentPointer - 1)) {
                    currentPointer--;
                }
                break;
            }
        }
    }

    /**
     * 
     * @param oldA
     * @param newA
     * @return
     */
    public int update(String oldA, String newA) {
        int i;
        for (i = 0; i < registerString.length; i++) {
            if (registerString[i].compareTo(oldA) == 0) {
                registerString[i] = newA;
            }
        }
        return i;
    }

    private int addToDifferentPlace(String clientAdress) {
        int i = 0;
        for (i = 0; i < currentPointer; i++) {
            if (registerInt[i] < 0) {
                break;
            }
        }
        System.out.println("Dodano w inne miejsce " + i);
        registerString[i] = clientAdress;
        registerInt[i] = 1;
        deleted = false;
        if (i == currentPointer) {
            currentPointer++;
        }
        return i + 1;
    }
}
